﻿using CommonLibrary;
using CommonLibrary.Models;
using CommonSite.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommonHelper;

namespace CommonSite.Areas.WF.Controllers
{
    [AdminAuthorizeAttribute]
    public class HomeController : Controller
    {
        //
        // GET: /WF/Home/

        public ActionResult Index()
        {
            return View();
        }

        [ChildActionOnly]
        public PartialViewResult Accordion()
        {
            return PartialView();
        }

        public ActionResult Forms()
        {
            return View();
        }

        public ActionResult Flows()
        {
            return View();
        }

        public ActionResult Fields(string id)
        {
            if (id.IsNullOrWhiteSpace())
                return RedirectToAction("Forms");

            using (CommonDatabase db = new CommonDatabase(CommonOption.Read))
            {
                try
                {
                    int formId = CryptHelper.DecryptToInt32(id);
                    if (db.wf_Form.Where(k => k.Id == formId && k.State == 1)
                          .Count() == 0)
                    {
                        return RedirectToAction("Forms");
                    }
                }
                catch
                {
                    return RedirectToAction("Forms");
                }
            }

            ViewBag.FormId = id;
            return View();
        }

        public ActionResult Dictionaries()
        {
            return View();
        }

        public ActionResult Nodes(string id)
        {
            if (id.IsNullOrWhiteSpace())
                return RedirectToAction("Flows");

            using (CommonDatabase db = new CommonDatabase(CommonOption.Read))
            {
                try
                {
                    int flowId = CryptHelper.DecryptToInt32(id);
                    if (db.wf_Flow.Where(k => k.Id == flowId)
                          .Count() == 0)
                    {
                        return RedirectToAction("Flows");
                    }

                    this.ViewBag.Roles = (from p in db.sys_Role
                                          select new CommonSite.Areas.API.Models.Role
                                          {
                                              Id = CryptHelper.Encrypt(p.Id),
                                              RoleName = p.Name
                                          }).ToList();
                }
                catch
                {
                    return RedirectToAction("Flows");
                }
            }

            ViewBag.FlowId = id;
            return View();
        }
    }
}